package com.voole.fragments;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.astuetz.viewpager.extensions.sample.R;
import com.voole.adapters.LocalAppAdapter;

public class LocalAppFragment extends Fragment {

	public static LocalAppFragment newInstance() {
		LocalAppFragment f = new LocalAppFragment();
		return f;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mLocalAppList = new ArrayList<AppInfo>();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View mView = inflater.inflate(R.layout.root_fragment_localapps, container, false);
		return mView;
		
	}
	View rootView;
	GridView mGridView;
	LocalAppAdapter mLocalAppAdapter;
	List<AppInfo> mLocalAppList;
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		rootView = getView();
		mGridView = (GridView) rootView.findViewById(R.id.gv_local_apps);
		mGridView.post(new Runnable() {
			
			@Override
			public void run() {
			readLocalApps(getActivity(), mLocalAppList);
			initAdapter();
			}
		});
	}
	private void initAdapter()
	{
		if(mLocalAppAdapter==null){
			mLocalAppAdapter = new LocalAppAdapter(getActivity(), mLocalAppList);
			mGridView.setAdapter(mLocalAppAdapter);;
		}else{
			mLocalAppAdapter.notifyDataSetChanged();
		}
	}
	
	//------------>>--------------------------------------------------------------------------------------------------------------
	public final static void readLocalApps(Context ctx,List<AppInfo> buflist){
		//调用
		PackageManager pm = ctx.getPackageManager();
		List<PackageInfo> apkInfos = pm.getInstalledPackages(0);
		getInfos(pm,apkInfos,buflist);
	}

	private final static void getInfos(PackageManager pm,List<PackageInfo> apkInfos, List<AppInfo> infos_name) {
		if(infos_name==null){
			infos_name = new ArrayList<AppInfo>();
		}
		for (PackageInfo pInfo : apkInfos) {
			String name = (String) pm.getApplicationLabel(pInfo.applicationInfo);
			Drawable icon = pm.getApplicationIcon(pInfo.applicationInfo);
			AppInfo mAm = new AppInfo();
			mAm.setName(name);
			mAm.setIcon(icon);
			infos_name.add(mAm);
		}
	}
	public final static class AppInfo{
		private String name;
		private Drawable icon;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public Drawable getIcon() {
			return icon;
		}
		public void setIcon(Drawable icon) {
			this.icon = icon;
		}
	}
	//------------>>----------------------------------------------------------------------------------------------------------------
}
